/*=============================================================================

  Application UI MODULE

  FILE: UI_evt.cpp
  
  DESCRIPTION

    Application UI Module

  EXTERNALIZED FUNCTIONS


   Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who       what, where, why
  ---------  -------  -------------------------------------------------------
   03/14/04   nso4th    Initial version

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <Cust_AppDef.h>
#include "mainView.h"
#include "mainFrm.h"
#include "dlgEng.h"

/*=============================================================================
  STRUCTURE DEFINITION
=============================================================================*/
typedef struct
{
	char		szcmd[64];
	void(*cmdfunc)(char *arg);
} ui_cmd_s;

/*=============================================================================
  LOCALIZED FUNCTION DEFINITION
=============================================================================*/
static void ui_cmd_string(char *pszstr);
static boolean ui_cmd_parse(char *strcmd, char *szcmd, char *szarg);

//-------------------------------------------------------------------
// GENERAL COMMAND HANDLER
//-------------------------------------------------------------------
static void ui_exit(char *arg);
static void ui_clear(char *arg);
static void ui_ver(char *arg);
static void ui_diag(char *arg);
static void ui_stop(char *arg);
static void ui_mem(char *arg);
static void ui_test(char *arg);
static void ui_view(char *arg);

extern void vwGra_Show(boolean enable);
extern void vwGra_SetData(VECTOR v1, VECTOR v2);
extern void vwGra_Clear(void);

/*=============================================================================
  LOCALIZED VARIABLE DEFINITION
=============================================================================*/
static ui_cmd_s	gui_cmd[] = \
{
	// General command
	{"exit", ui_exit},
	{"clear", ui_clear},
	{"ver", ui_ver},
	{"diag", ui_diag},
	{"stop", ui_stop},
	{"mem", ui_mem},
	{"test", ui_test},
	{"view", ui_view},
};

static char	gstr[128];

/*=============================================================================
  EXTERNALIZED FUNCION DEFINITION
=============================================================================*/
void UIcmd(char *strcmd)
{
	int32			cnt, i;
	ui_cmd_s		*ptr;
	static char		szcmd[64], szarg[64];

	memZeros(szcmd, sizeof(szcmd));
	memZeros(szarg, sizeof(szarg));
	if(ui_cmd_parse(strcmd, szcmd, szarg) == FALSE)
	{
		return;
	}
	
	cnt = sizeof(gui_cmd)/sizeof(ui_cmd_s);
	for(ptr = &gui_cmd[0], i=0; i<cnt; i++, ptr++)
	{
		if(strcmp(ptr->szcmd, szcmd) == 0)
		{
			ptr->cmdfunc(szarg);
			return;
		}
	}
	ui_cmd_string("Abnormal command ...");
}

/*=============================================================================
  LOCALIZED FUNCTION IMPLEMENTATION
=============================================================================*/
void ui_cmd_string(char *pszstr)
{
	CDlgEng		*dlg = (CDlgEng*)ObjList_Get(VIEW_ENG);

	if(dlg)
	{
		GText		*gt = &dlg->m_gTxt[DEN_TXT_CMD];

		gt->SetText(pszstr);
	}
}

boolean ui_cmd_parse(char *strcmd, char *szcmd, char *szarg)
{
	int32		i, j, n;
	
	if((strcmd == NULL) || (strlen(strcmd) == 0))
		return	FALSE;

	n = strlen(strcmd);
	
	for(i=0; i<n; i++)
	{
		if((*(strcmd+i)) == ' ')
		{
			break;
		}
		*(szcmd+i) = *(strcmd+i);
	}
	for(i++, j=0; i<n; i++, j++)
	{
		*(szarg+j) = *(strcmd+i);
	}
	
	return	TRUE;
}

void ui_exit(char *arg)
{
	UIvw_Eng(FALSE);
}

void ui_clear(char *arg)
{
	ui_cmd_string("Ready !");
}

void ui_ver(char *arg)
{
	char		*str = &gstr[0];

#ifdef _ODEBUG_
	sprintf(str, "Version: %s.%s.%003dD (%s)", CUST_APP_NAME_SHORT, TG_APP_VERSION, TG_APP_BUILD, TG_APP_BUILD_DATE);
#else
	sprintf(str, "Version: %s.%s.%003d (%s)", CUST_APP_NAME_SHORT, TG_APP_VERSION, TG_APP_BUILD, TG_APP_BUILD_DATE);
#endif
	ui_cmd_string(str);
}

static void ui_diag_on(void);
static void ui_diag_off(void);
void ui_diag(char *arg)
{	
	if(strcmp(arg, "on") == 0)
	{
		ui_diag_on();
		ui_clear("");
	}
	else if(strcmp(arg, "off") == 0)
	{
		ui_diag_off();
		ui_clear("");
	}
	else
	{
		ui_cmd_string("Abnormal argument ...");
	}
}

void ui_diag_on(void)
{
	DBGINIT(NULL);
}

void ui_diag_off(void)
{
	DBGCLOSE();
}

void ui_stop(char *arg)
{
	SYSmgr			*sm = (SYSmgr*)ObjList_Get(SYS_MGR);

	if(sm)
	{
		sm->Stop();
	}
}

/*=============================================================================
  APPLICATION & TEST PURPOSE
=============================================================================*/
void ui_mem(char *arg)
{
	if(strcmp(arg, "dump") == 0)
	{
		SYSMEM_DUMP(0);
	}
	else if(strcmp(arg, "clear") == 0)
	{
		SYSMEM_CLEAR();
	}
	else if(strcmp(arg, "on") == 0)
	{
		SYSMEM_Show(TRUE);
	}
	else
	{
		SYSMEM_Show(FALSE);
	}
}

#if 1
void ui_test(char *arg)
{
	
}

#else
extern MATRIX RA2LOWPASS(MATRIX *Af, MATRIX R, MATRIX A, double64 cutoff);
extern MATRIX RA2CONV(MATRIX *Af, MATRIX R, MATRIX A, int32 mx, int32 my);

void ui_test(char *arg)
{
	MATRIX			R, A, Rt, At, Rf, Af, Rc, Ac;
	double64			cut;

	cut =0.4;

	DBGPRINT("cut = %.3f\n", cut);
	
	R = Mat_Load("D:\\tmp\\R.txt");
	A = Mat_Load("D:\\tmp\\A.txt");

	Rt = Mat_Transpose(R);
	At = Mat_Transpose(A);
	
	Rf = RA2LOWPASS(&Af, Rt, At, cut);
	Rc = RA2CONV(&Ac, Rt, At, 3, 3);

	Mat_SetSaveDigit(FALSE, 3);
	Mat_SaveASCII(Rf, "D:\\tmp\\Rf.txt");
	Mat_SaveASCII(Af, "D:\\tmp\\Af.txt");

	Mat_SaveASCII(Rc, "D:\\tmp\\Rc.txt");
	Mat_SaveASCII(Ac, "D:\\tmp\\Ac.txt");
	
	MAT_FREE(A);
	MAT_FREE(R);

	MAT_FREE(At);
	MAT_FREE(Rt);
	
	MAT_FREE(Af);
	MAT_FREE(Rf);

	MAT_FREE(Ac);
	MAT_FREE(Rc);
}
#endif

static void ui_view(char *arg)
{
	if(strcmp(arg, "vmanual") == 0)
	{
		UIvw_Select(UI_MANUAL_VW);
	}
	else if(strcmp(arg, "vsetup") == 0)
	{
		UIvw_Select(UI_SETUP_VW);
	}
	else
	{
		UIvw_Select(UI_MAIN_VW);
	}
	
}

/*--- END OF UI.cpp ---*/
